﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;


namespace CsvHelperPlus
{
    public class CsvHandler
    {

        public static DataTable ReadCSV(string path, bool hasTitle = true)
        {
            DataTable dt = new DataTable();
            StreamReader sr = new StreamReader(path, Encoding.Default);
            string line;
            string[] items = null;
            while ((line = sr.ReadLine()) != null)
            {
                items = line.Split(',');
                if (hasTitle)
                {
                    for (int i = 0; i < items.Length; i++)
                    {
                        dt.Columns.Add(new DataColumn(items[i]));
                    }
                    hasTitle = false;
                }
                else 
                {
                    dt.Rows.Add(items);
                }
            }
            sr.Close();
            return dt;
        }

        public static DataTable ReadCsvToDataTable(string path, bool hasTitle = true, Dictionary<string, Type> convert=null)
        {
            DataTable dt = new DataTable();
            if (convert == null)
            {
                convert = new Dictionary<string, Type>();
            }
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                {
                    string line;
                    Dictionary<int, string> colmaps = new Dictionary<int, string>();
                    string[] items = null;
                    while ((line = sr.ReadLine()) != null)
                    {
                        items = line.Split(',');
                        if (hasTitle)
                        {
                            for (int i = 0; i < items.Length; i++)
                            {
                                if (convert.ContainsKey(items[i]))
                                {
                                    dt.Columns.Add(items[i], convert[items[i]]);
                                }
                                else
                                {
                                    dt.Columns.Add(items[i], typeof(string)); //默认为string
                                }
                                colmaps.Add(i, items[i]);
                            }
                            hasTitle = false;
                            continue;
                        }
                        DataRow dr = dt.NewRow();
                        for (int i = 0; i < items.Length; i++)
                        {
                            if (convert.ContainsKey(colmaps[i]))
                            {
                                dr[i] = Convert.ChangeType(items[i], convert[colmaps[i]]);
                            }
                            else
                            {
                                dr[i] = items[i];
                            }
                        }
                        dt.Rows.Add(dr);

                    }
                }
            }
            return dt;
        }

        public static List<string> ReadCSV2List(string path, bool hasTitle = true)
        {
            List<string> dt = new List<string>(); 
            StreamReader sr = new StreamReader(path, Encoding.Default);
            string line;
            //string[] items = null;
            while ((line = sr.ReadLine()) != null)
            {
                //items = line.Split(',');
                dt.Add(line);
            }
            sr.Close();
            return dt;

        }


        public static void ListSave2CSV<T>(string path, List<T> list)
        {
            StreamWriter sr = new StreamWriter(path, false, Encoding.Default);
            var props = typeof(T).GetFields();
            string line = null;
            foreach (var p in props) //标题行
            {
                string value = p.Name;
                if (line == null)
                {
                    line = value;
                }
                else
                {
                    line += "," + value;
                }
            }
            sr.WriteLine(line);
            line = null;

            foreach (var r in list)
            {
                foreach(var p in props){
                    var obj = p.GetValue(r);
                    string value;
                    if (obj == null)
                    {
                        value = "";
                    }
                    else
                    {
                        value = obj.ToString();
                    }
                    if(line == null){
                        line = value;
                    }
                    else
                    {
                        line += "," + value;
                    }
                }
                sr.WriteLine(line);
                line = null;
            }
            sr.Close();
        }

        //public static System.Text.Encoding GetType(System.IO.FileStream fs)
        //{
        //    return
        //}

    }
}
